<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.4);
            /* margin: 100px; */
            /* transform: translate(); */
            /* position: absolute; */
            position: absolute;
            left: 100px;
            top: 100px;
            user-select: none;
        }

        .sub {
            width: 100px;
            height: 100px;
            background-color: #6ff;
            margin: 50px;
            pointer-events: none;
        }
    </style>
</head>

<body>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
</body>
<script>
    // 拖拽三剑客
    // onmousedown   
    // onmousemove
    // onmouseup

    // 面向对象
    // 思想上提升, 就是将你的需求抽象成一个对象, 然后针对这个对象分析其特征(属性)与行为(方法)--这个对象就称之为类(面向对象)

    // 元素拖拽
    // 1. 有元素
    // 2. 开始拖拽
    // 3. 拖拽移动
    // 3. 拖拽停止
    // debugger;
    function DragEle(ele) {
        // debugger;
        // {}
        // this -> {}

        this.target = ele;

        this.init();

        // this[[prototype]] = DragEle.prototype
        // return this;
    }

    DragEle.prototype.init = function () {
        // this -> 调用此方法的实例化对象
        this.randColor();
        this.dragStart();
    }
    DragEle.prototype.dragStart = function () {
        // this -> 调用此方法的实例化对象
        var _this = this;
        this.target.onmousedown = function (e) {
            var e = e || window.event;
            var posX = e.offsetX;
            var posY = e.offsetY;
            console.log(posX, posY);

            // console.log(this); // 指向绑定事件的元素

            _this.posX = posX;
            _this.posY = posY;
            console.log(_this);

            _this.target.style.zIndex = "99999";

            _this.dragMove(); // 先按下再移动
            _this.dragEnd();  // 按下时绑定清除
        }
    }
    DragEle.prototype.dragMove = function () {
        document.onmousemove = (e) => {
            var e = e || window.event;
            var x = e.pageX - this.posX;
            var y = e.pageY - this.posY;

            console.log(x, y);

            this.target.style.left = x + "px";
            this.target.style.top = y + "px";


        }
    }
    DragEle.prototype.dragEnd = function () {
        document.onmouseup = function () {
            document.onmousemove = null;
            this.target.style.zIndex = 0;
        }.bind(this);
    }
    DragEle.prototype.randColor = function () {
        var colorStr = "#";
        for (var i = 0; i < 6; i++) {
            var num = Math.round(Math.random() * 16);
            var char = num.toString(16);
            colorStr += char;
        }
        this.target.style.backgroundColor = colorStr;
    }


    var shadowList = document.getElementsByClassName("shadow");

    for (let i = 0; i < shadowList.length; i++) {
        let shadow = shadowList[i];
        var obj = new DragEle(shadow);
        console.log(obj);
        // obj.init();
    }




</script>

</html>